// 具体公文
$(document).ready(function () {
    //获得参数的方法
    var request =
    {
        QueryString : function(val)
        {
            var uri = window.location.search;
            var re = new RegExp("" +val+ "=([^&?]*)", "ig");
            return ((uri.match(re))?(uri.match(re)[0].substr(val.length+1)):null);
        }
    }

    var doc_id = request.QueryString("doc_id");
    //
    $.ajax({
        type: 'GET',//请求类型
        url: '../document/query/detail/' + doc_id,
        dataType: 'JSON',
        success: function (json,status) {
            if(json.err_msg == null){
                //调用方法获得参数
                if(json.data!=null){
                    $("#documentIframe").css("display","block");
                    iFrameHeight();
                    $("#documentIframe").attr("src","../assets/js/document/pdfjs-dist/web/viewer.html?file=/document/query/read/" + doc_id);

                }else{
                    $("#draft_pdf").css("display","none");
                }
            }else{
                alert(json.err_msg);
            }

        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            console.log(textStatus);
        }
    });

    // 获取所有附件列表
    $.ajax({
        dataType:'JSON',
        type: 'POST',
        url: '../document/review/annex/list/' + doc_id,
        dataType: 'JSON',
        success: function (json,status){
            //console.log(json);
            if(json.err_msg != null){
                alert(json.err_msg);
            }else{
                var documentAnnex = json.data.documentAnnex;
                for(var i = 0;i<documentAnnex.length;i++){
                    $("#annexTable").append("<tr>" +
                        "<td>"+documentAnnex[i].name+"</td>" +
                        "<td>"+documentAnnex[i].writer+"</td>" +
                        "<td><button type='button' class='btn btn-success' onclick='downFujian("+ documentAnnex[i].id +")'>下载</button>" +
                        "</td>");
                }
                //console.log(json.data.documentAnnex);d
            }
        },
        error: function (XMLHttpRequest, textStatus) {
            console.log(textStatus);
        },
        fail: function () {
            console.log("fail!");
        }
    });


    //iframe自适应
    function iFrameHeight() {
        var ifm= document.getElementById("documentIframe");
        var subWeb = document.frames ? document.frames["documentIframe"].document : ifm.contentDocument;
        if(ifm != null && subWeb != null) {
            //ifm.height = subWeb.body.scrollHeight;
            ifm.width = subWeb.body.scrollWidth;
        }
    }

    // 公文下载
    $("#documentDown").click(function () {
        //console.log
        window.location.href='../document/query/down/' + doc_id;
    })

});

//downFujian
function downFujian(id) {
    window.location.href="../document/annex/down/" + doc_id + "/" + id;
}